package com.wwwarehouse.contract.order_information;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.adapter.pop.BubblePopAdapter;
import com.wwwarehouse.common.bean.carddesk.CardDeskMessageStableCardBean;
import com.wwwarehouse.common.bean.filter.FilterBean;
import com.wwwarehouse.common.bean.filter.SeriesBean;
import com.wwwarehouse.common.eventbus_event.DrawerSeriesCompleteEvent;
import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.common.fragment.DeskDrawerSeriesFragment;
import com.wwwarehouse.common.tools.PopUpUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.contract.bean.BuyBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Creator by zhenwang.xiang on 2017/11/2 16:03
 * Description: 我买到的Viewpager页面
 */


@Route(path = ContractConstant.PATH_BUYVIEWPAGERFRAGMENT)
public class BuyViewPagerFragment extends CommonBasePagerFragment {

    private List<SeriesBean> mFilterList;
    private String sort = "a.contract_time desc";
    private String businessName = "";

    private String orderState = "";
    private int mOrderType = 0;
    private List<FilterBean> mfilterBeen;
    private ArrayList<String> mBusinessIds = new ArrayList<>();
    private ArrayList<String> mOrderStateList = new ArrayList<>();
    private List<CardDeskMessageStableCardBean.TasksBeanBuidNames> mTasksBeanBuidNames;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(DrawerSeriesCompleteEvent event) {
        if (peekFragment() instanceof BuyViewPagerFragment) {
            List<SeriesBean> eventList = event.getList();
            boolean reset = event.isReset();
            if (eventList != null) {
                List<FilterBean> beanList = eventList.get(0).getList();
                mBusinessIds.clear();
                for (int i = 0; i < beanList.size(); i++) {
                    if (beanList.get(i) != null) {
                        if (beanList.get(i).isSelect()) {
                            CardDeskMessageStableCardBean.TasksBeanBuidNames tasksBeanBuidNames = (CardDeskMessageStableCardBean.TasksBeanBuidNames) beanList.get(i).getData();
                            mBusinessIds.add(tasksBeanBuidNames.getId());
                        }
                    }
                }
                if (mBusinessIds.size() == 0) {
                    if (mTasksBeanBuidNames != null) {
                        for (int i = 0; i < mTasksBeanBuidNames.size(); i++) {
                            mBusinessIds.add(mTasksBeanBuidNames.get(i).getId());
                        }
                    }
                }
                List<FilterBean> beanList1 = eventList.get(1).getList();
                mOrderStateList.clear();
                if (beanList1.get(0).isSelect()) {
                    orderState = "10";
                    mOrderStateList.add(orderState);
                }
                if (beanList1.get(1).isSelect()) {
                    orderState = "440";
                    mOrderStateList.add(orderState);
                }
                if (beanList1.get(2).isSelect()) {
                    orderState = "450";
                    mOrderStateList.add(orderState);
                }
                if (beanList1.get(3).isSelect()) {
                    orderState = "500";
                    mOrderStateList.add(orderState);
                }
                if (beanList1.get(4).isSelect()) {
                    orderState = "900";
                    mOrderStateList.add(orderState);
                }
            }
            if (!reset) {
                requestHttp();
            }

        }
    }

    @Override
    public void requestDatas() {
        super.requestDatas();

        if (getArguments() != null) {
            CardDeskMessageStableCardBean.TasksBean tasksBean = getArguments().getParcelable("data");
            if (tasksBean != null) {
                mTasksBeanBuidNames = tasksBean.getTasksBeanBuidNames();
            }
            if (getArguments().getInt("flag") != 0) {
                mOrderType = getArguments().getInt("flag");
                if (mOrderType == 1) {
                    ((BaseCardDeskActivity) mActivity).setTitle("我买到的");
                } else if (mOrderType == 2) {
                    ((BaseCardDeskActivity) mActivity).setTitle("我卖出的");
                }
            }
        }

        mFilterList = new ArrayList<>();
        SeriesBean seriesBean = new SeriesBean();
        seriesBean.setTitle("所属业务单位");
        seriesBean.setCount(1);
        seriesBean.setSingle(false);
        List<FilterBean> list = new ArrayList<>();
        if (mTasksBeanBuidNames != null) {
            for (int i = 0; i < mTasksBeanBuidNames.size(); i++) {
                mBusinessIds.add(mTasksBeanBuidNames.get(i).getId());
                list.add(new FilterBean(false, mTasksBeanBuidNames.get(i).getName(), mTasksBeanBuidNames.get(i)));
            }
        }

        seriesBean.setList(list);
        mFilterList.add(seriesBean);

        SeriesBean seriesBean1 = new SeriesBean();
        seriesBean1.setTitle("交易状态");
        seriesBean1.setCount(2);
        seriesBean1.setSingle(false);
        List<FilterBean> list1 = new ArrayList<>();
        list1.add(new FilterBean(false, "待付款"));
        list1.add(new FilterBean(false, "待发货"));
        list1.add(new FilterBean(false, "待收货"));
        list1.add(new FilterBean(false, "交易完成"));
        list1.add(new FilterBean(false, "交易关闭"));
        seriesBean1.setList(list1);
        mFilterList.add(seriesBean1);

        DeskDrawerSeriesFragment deskDrawerPowerFragment = DeskDrawerSeriesFragment.newInstance(mFilterList);
        ((BaseCardDeskActivity) mActivity).setDrawerLayout(deskDrawerPowerFragment);

        mfilterBeen = new ArrayList<>();
        mfilterBeen.add(new FilterBean(true, "下单时间从近到远"));
        mfilterBeen.add(new FilterBean(false, "下单时间从远到近"));
        mfilterBeen.add(new FilterBean(false, "订单金额从多到少"));
        mfilterBeen.add(new FilterBean(false, "订单金额从少到多"));

        //网络请求
        requestHttp();
    }

    //过滤
    @Override
    public void filterClick() {
        super.filterClick();
        if (peekFragment() instanceof BuyViewPagerFragment) {
            ((BaseCardDeskActivity) mActivity).showDrawerLayout();
        }
    }

    //排序
    @Override
    public void sortClick(View anchor) {
        super.sortClick(anchor);
        if (peekFragment() instanceof BuyViewPagerFragment) {
            PopUpUtils.showBubbleDown(anchor, mActivity, mfilterBeen, new BubblePopAdapter.OnPopListClickListener() {

                @Override
                public void setOnItemClickListener(int i, View view) {
                    switch (i) {
                        case 0:
                            mfilterBeen.set(0, new FilterBean(true, "下单时间从近到远"));
                            mfilterBeen.set(1, new FilterBean(false, "下单时间从远到近"));
                            mfilterBeen.set(2, new FilterBean(false, "订单金额从多到少"));
                            mfilterBeen.set(3, new FilterBean(false, "订单金额从少到多"));
                            sort = "a.contract_time desc";
                            break;
                        case 1:
                            mfilterBeen.set(0, new FilterBean(false, "下单时间从近到远"));
                            mfilterBeen.set(1, new FilterBean(true, "下单时间从远到近"));
                            mfilterBeen.set(2, new FilterBean(false, "订单金额从多到少"));
                            mfilterBeen.set(3, new FilterBean(false, "订单金额从少到多"));
                            sort = "a.contract_time";
                            break;
                        case 2:
                            mfilterBeen.set(0, new FilterBean(false, "下单时间从近到远"));
                            mfilterBeen.set(1, new FilterBean(false, "下单时间从远到近"));
                            mfilterBeen.set(2, new FilterBean(true, "订单金额从多到少"));
                            mfilterBeen.set(3, new FilterBean(false, "订单金额从少到多"));
                            sort = "c.total_amount desc";
                            break;
                        case 3:
                            mfilterBeen.set(0, new FilterBean(false, "下单时间从近到远"));
                            mfilterBeen.set(1, new FilterBean(false, "下单时间从远到近"));
                            mfilterBeen.set(2, new FilterBean(false, "订单金额从多到少"));
                            mfilterBeen.set(3, new FilterBean(true, "订单金额从少到多"));
                            sort = "c.total_amount";
                            break;
                        default:
                            mfilterBeen.set(0, new FilterBean(true, "下单时间从近到远"));
                            mfilterBeen.set(1, new FilterBean(false, "下单时间从远到近"));
                            mfilterBeen.set(2, new FilterBean(false, "订单金额从多到少"));
                            mfilterBeen.set(3, new FilterBean(false, "订单金额从少到多"));
                            sort = "a.contract_time desc";
                            break;
                    }
                    requestHttp();
                }
            });
        }
    }

    //搜索
    @Override
    public void textChanged(String text) {
        super.textChanged(text);
        businessName = text;
        requestHttp();
    }

    //取消
    @Override
    public void cancelClick() {
        super.cancelClick();
        businessName = "";
        requestHttp();
    }

    private void requestHttp() {
        Map<String, Object> map = new HashMap<>();
        map.put("businessIds", mBusinessIds);
        map.put("orderType", mOrderType);
        map.put("page", 1);
        map.put("size", "5");
        map.put("sort", sort);

        if (!TextUtils.isEmpty(businessName)) {
            map.put("businessName", businessName);
        }
        if (mOrderStateList.size() > 0) {
            map.put("orderStatus", mOrderStateList);
        }

        loadData(ContractConstant.GET_MYORDER, map, 0);

    }

    @Override
    public void setTitle() {
        super.setTitle();
        if (peekFragment() instanceof BuyViewPagerFragment) {
            if (mOrderType == 1) {
                mActivity.setTitle("我买到的");
            } else if (mOrderType == 2) {
                mActivity.setTitle("我卖出的");
            }
        }
    }


    @Override
    public void onLoadSuccess(String data, int what) {
        if (0 == what) {
            BuyBean bean = JSON.parseObject(data, BuyBean.class);
            if (null != bean) {
                Bundle bundle = new Bundle();
                bundle.putStringArrayList("businessIds", mBusinessIds);
                bundle.putString("businessName", businessName);
                bundle.putStringArrayList("orderStatus", mOrderStateList);
                bundle.putInt("orderType", mOrderType);
                bundle.putString("sort", sort);
                try {
                    setData(bean.getTotal(), 5, "com.wwwarehouse.contract.order_information.BuyFragment", bundle, bean.getList());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onRefreshListener() {
        requestHttp();
    }

    @Override
    public void onLoadError(String errorInfo) {
        ToastUtils.showToast(errorInfo);
    }

}
